<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRoleFn"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" border>
              <el-table-column label="序号" style="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="setAuth(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination :total="total" :page="page.pagesize" layout="prev,pager,next" @current-change="changePage" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="info.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="info.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="info.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="info.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog :title="formData.id ? '编辑角色' : '添加角色'" :visible="showDialog" @close="handleClose">
      <el-form ref="form" :rules="rules" :model="formData" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="handleClose">取消</el-button>
          <el-button size="small" type="primary" @click="submit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 权限 -->
    <set-auth ref="setAuth" />
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/setting'
import { mapGetters } from 'vuex'
import setAuth from './components/set-auth.vue'
export default {
  components: { setAuth },
  data() {
    return {
      activeName: 'first',
      page: {
        page: 1,
        pagesize: 5
      },
      total: 0, // 记录总数
      list: [], // 角色列表信息
      info: {}, // 公司信息
      showDialog: false,
      formData: {
        description: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo() // 获取公司信息
  },
  methods: {
    // 打开权限弹窗
    async setAuth(roleId) {
      // 调用获取所有权限的方法
      await this.$refs.setAuth.getPermissionList()
      // 调用获取当前权限的方法
      await this.$refs.setAuth.getRoleDetail(roleId)
      // 关闭弹窗
      this.$refs.setAuth.diaLogisShow = false
    },
    // 点击添加打开弹窗
    addRoleFn() {
      this.showDialog = true
    },
    // 新增或者更新
    async  submit() {
      this.$refs.form.validate()
      if (this.formData.id) {
        // 更新
        await updateRole(this.formData)
      } else {
        // 新增
        await addRole(this.formData)
      }
      this.$message.success('操作成功！')
      this.getRoleList()
      this.handleClose()
    },
    // 关闭弹窗
    handleClose() {
      this.formData = {
        description: '',
        name: ''
      }
      this.$refs.resetFields()
      this.showDialog = false
    },
    // 编辑角色
    async editRole(id) {
      this.showDialog = true
      const res = await getRoleDetail(id)
      this.formData = res
    },
    // 删除角色
    async delRole(id) {
      try {
        await this.$confirm('确定删除该角色？')
        await deleteRole(id)
        if (this.list.length === 1 && this.page.page !== 1) {
          this.page.page--
        }
        this.getRoleList()
        this.$message.success('删除成功！')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取公司信息
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      this.formData = res
    },
    // 获取角色列表
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.total = total
    },
    // 点击获取当前页码
    changePage(page) {
      this.page.page = page
      this.getRoleList()
    }
  }
}
</script>

<style>

</style>
